package com.ease.framework.gef4p.commands {
   import com.ease.framework.gef.IEditor;
   import com.ease.framework.gef4p.editparts.EdgeEditPart;
   import com.ease.framework.gef4p.editparts.NodeEditPart;
   import com.ease.framework.gef4p.model.Edge;
   import com.ease.framework.gef4p.model.Node;
   import com.ease.framework.workbench.Workbench;
   import com.ease.framework.workbench.spi.impls.Command;

   public class MoveEdgeSourceAnchorCommand extends Command {
      private var originalSourceNode:Node;
      private var currentSourceNode:Node;
      private var nowEdge:Edge;

      private var tranEP:EdgeEditPart;


      public function MoveEdgeSourceAnchorCommand(edge:Edge, node:Node) {
         super();
         nowEdge = edge;
         originalSourceNode = nowEdge.source;
         currentSourceNode = node;
      }

      override public function execute():void {

         if (tranEP == null) {
            tranEP = IEditor(Workbench.instance.currentEditor).getEditDomain().getEditPartByModel(nowEdge) as EdgeEditPart;
         }
         tranEP.getSource().removeOutEdge(tranEP);

         nowEdge.source = currentSourceNode;

         var sourceEP:NodeEditPart = IEditor(Workbench.instance.currentEditor).getEditDomain().getEditPartByModel(currentSourceNode) as NodeEditPart;
         tranEP.setSource(sourceEP);

         tranEP.refreashFigure();
      }

      override public function undo():void {
         tranEP.getSource().removeOutEdge(tranEP);

         nowEdge.source = originalSourceNode;

         var sourceEP:NodeEditPart = IEditor(Workbench.instance.currentEditor).getEditDomain().getEditPartByModel(originalSourceNode) as NodeEditPart;
         tranEP.setSource(sourceEP);

         tranEP.refreashFigure();

      }

   }
}